package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMchntImageInfoMapper;
import cc.rengu.igas.bsps.common.entity.MchntImageInfoBean;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/04/09
 */
public class BspsMchntImageInfoMapperImpl implements BspsMchntImageInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<MchntImageInfoBean> selectBspsMchntSignImageInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntSignImageInfoByMchntNo");
        MchntImageInfoBean mchntImageInfo = new MchntImageInfoBean();
        mchntImageInfo.setInstId(instId);
        mchntImageInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntImageInfo, MchntImageInfoBean.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号<{}>获取商户签约影像信息失败!", mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntImageInfoBean) (item)).collect(Collectors.toList());
    }
}
